
/*
 * Problem: 看图写树(Undraw the Trees)
 * Author: Yuanshun L
 * Created: 9-Nov-2021
 * Method: Deep-First Search
 */

#include<iostream>
#include<string>

using namespace std;

#define maxn 100+5
int n;


string graph[maxn];

// 搜索floor层次（从0开始）pos位置节点的孩子
// 深度优先遍历
void write(int pos,int floor){
    // 有孩子
    cout << graph[floor][pos] << "(";
    if(floor < n-1 && graph[floor+1][pos] == '|'){
        int start = pos;
        // 找到该分支最左边能搜索的起点
        while(start - 1>= 0 && graph[floor+2][start-1] == '-') start--;
        // 从起点直到空白，搜索所有节点
        for(int i=start; graph[floor+2][i] == '-';i++){
            if(graph[floor+3][i] != ' '){
                write(i,floor+3);
            }
        }
    }
    cout << ")";
}

int main(){


    freopen("data.in","r",stdin);
    freopen("data.out","w",stdout);

    int T;
    cin >> T;
    string blank;
    getline(cin,blank);
    while(T--){
        n = 0;
        while(getline(cin,graph[n]) && graph[n][0] != '#') n++;

        int start = 0; // 找到根结点
        for(; graph[0][start] == ' '; start++);


        cout << "(";
        // 从根结点开始搜索
        write(start,0);
        cout << ")";
        cout << endl;
    }

    return 0;
}